#!/usr/bin/env python

import os, sys
from comment_class import Comment

def add ( liwe, id_obj, module, txt ):
	"""
	:INPUT:
		- id_obj	SAFE_DB_NUMBER
		- module	SAFE_DB_STRING
		- txt		SAFE_DB_STRING
	"""
	
	c = Comment ( liwe )
		
	id_site = c.site_id #liwe.cfg.get ( 'SITE_ID' , 0 )
	id_comment = c.add_comment ( id_obj = id_obj, module = module, txt = txt, id_site = id_site )

	return { "id_comment" : id_comment }

def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1

	res [ 'rows' ] = tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = lines

	return res

def list ( liwe, id_obj = "", module = "", order_by = "",  _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_obj	- ID object to list SAFE_DB_STRING
		- module	- SAFE_DB_STRING
		- order_by	- SAFE_STRING
		- _X_LINES:	SAFE_NUMBER
		- _X_START_POINT: SAFE_NUMBER
		- _X_PAGE: SAFE_NUMBER
	"""
	use_dataset = 0
	quant = 1

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	c = Comment ( liwe )

	id_site = c.site_id
	res = c.list ( id_obj = id_obj, module = module, order_by = order_by, id_site = id_site , start_point = start_point, do_count = use_dataset )

	rows = res [ 'rows' ]
	tot_rows = res [ 'tot_rows' ]

	if not use_dataset: 
		if not rows: rows = []
		return { 'comments': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )

def get_templates ( liwe ):
	"""
	:INPUT:
	"""
	c = Comment ( liwe )
        return { "templates" : c.get_templates () }

